﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BinhSon.Services.Const;
using BinhSon.Services.Contracts;
using BinhSon.Services.Impl;

namespace BinhSon.Presentation
{
    public partial class ThaoTacDiemThi : UserControl
    {
        private readonly IBinhSonServices _bsServices;

        public ThaoTacDiemThi()
        {
            InitializeComponent();
            this._bsServices = new BinhSonServices();
            this.LoadHocKy();
            this.LoadKhoi();
            this.LoadLanThi();
            this.LoadLoaiKiemTra();            
        }               

        private void nhapFileButton_Click(object sender, EventArgs e)
        {
            var dialog = new OpenFileDialog
            {
                Filter = "Excel Workbook(*.xlsx)|*.xlsx|Excel 97-2003 Workbook(*.xls)|*.xls"
            };
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                try
                {                    
                    var dataSet = ProcessExcelFile.ImportExcelXLS(dialog.FileName, true);
                    var result = this._bsServices.ImportDiemThi(dataSet);
                    MessageBox.Show(result ? "File đã được xử lý thành công." : "Xử lý file đã thất bại.");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk: " + ex.Message);
                }
            }            
        }

        private void xuatBangDiemButton_Click(object sender, EventArgs e)
        {
            var selectedHocKy = (ComboBoxModel) hocKyComboBox.SelectedItem;
            if (selectedHocKy == null)
            {
                MessageBox.Show("Vui lòng chọn học kỳ");
                return;
            }
            var selectedKhoi = (ComboBoxModel) khoiComboBox.SelectedItem;
            if (selectedKhoi == null)
            {
                MessageBox.Show("Vui lòng chọn khối");
                return;
            }
            var selectedMonHoc = (ComboBoxModel) monHocComboBox.SelectedItem;
            if (selectedMonHoc == null)
            {
                MessageBox.Show("Vui lòng chọn môn học");
                return;
            }
            var selectedLopHoc = (ComboBoxModel) lopHocComboBox.SelectedItem;
            var selectedLanThi = (ComboBoxModel) lanThiComboBox.SelectedItem;
            var selectedLoaiKiemTra = (ComboBoxModel) loaiKiemTraComboBox.SelectedItem;
            var lopHocId = selectedLopHoc == null ? (int?)null : selectedLopHoc.Key;
            var lanThiId = selectedLanThi == null ? (int?) null : selectedLanThi.Key;
            var heSo = selectedLoaiKiemTra == null ? (int?)null : selectedLoaiKiemTra.Key;


            var dialog = new SaveFileDialog
            {
                Filter = "Excel Workbook(*.xlsx)|*.xlsx|Excel 97-2003 Workbook(*.xls)|*.xls"
            };
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                try
                {                    
                    var dataSet = this._bsServices.ExportBangDiemThi(selectedHocKy.Key, selectedKhoi.Key, selectedMonHoc.Key, lopHocId, lanThiId, heSo);
                    var result = CreateExcelFile.CreateExcelDocument(dataSet, dialog.FileName);
                    MessageBox.Show(result ? "File đã được tạo thành công." : "Quá trình tạo file thất bại.");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not save file to disk: " + ex.Message);
                }
            }
        }        

        private void LoadHocKy()
        {
            var result = this._bsServices.ListHocKy().Select(x => new ComboBoxModel
            {
                Key = x.LookupReference,
                Value = x.Description
            });
            foreach (var comboBoxModel in result)
            {
                hocKyComboBox.Items.Add(comboBoxModel);
            }
        }

        private void LoadKhoi()
        {
            var result = this._bsServices.ListKhoi().Select(x => new ComboBoxModel
            {
                Key = x.LookupReference,
                Value = x.Description
            });
            foreach (var comboBoxModel in result)
            {
                khoiComboBox.Items.Add(comboBoxModel);
            }
        }

        private void LoadLanThi()
        {
            var result = this._bsServices.ListLanThi().Select(x => new ComboBoxModel
            {
                Key = x.LookupReference,
                Value = x.Description
            });
            foreach (var comboBoxModel in result)
            {
                lanThiComboBox.Items.Add(comboBoxModel);
            }
        }

        private void LoadMonHoc(ComboBoxModel selectedKhoi, ComboBox targetComboBox)
        {
            var result = this._bsServices.ListMonHoc(selectedKhoi.Key).Select(x => new ComboBoxModel
            {
                Key = x.MonHocId,
                Value = x.TenMonHoc
            });
            targetComboBox.Items.Clear();
            targetComboBox.Text = "";
            foreach (var comboBoxModel in result)
            {
                targetComboBox.Items.Add(comboBoxModel);
            }
        }

        private void LoadLopHoc(int khoiId)
        {
            var result = this._bsServices.GetListOfLopHoc(khoiId).Select(x => new ComboBoxModel
            {
                Key = x.LopHocId,
                Value = x.TenLop
            }).ToList();
            lopHocComboBox.Items.Clear();
            lopHocComboBox.Text = "";
            foreach (var comboBoxModel in result)
            {
                lopHocComboBox.Items.Add(comboBoxModel);
            }
        }

        private void khoiComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            var selectedKhoi = (ComboBoxModel)khoiComboBox.SelectedItem;
            if (selectedKhoi == null)
            {
                monHocComboBox.Items.Clear();
                monHocComboBox.Text = "";
            }
            else
            {
                this.LoadMonHoc(selectedKhoi, monHocComboBox);
                this.LoadLopHoc(selectedKhoi.Key);
            }
        }

        private void LoadLoaiKiemTra()
        {
            var result = this._bsServices.GetListOfHeSoDiem().Select(x => new ComboBoxModel
            {
                Key = x.LookupReference,
                Value = x.Description
            });
            foreach (var comboBoxModel in result)
            {
                loaiKiemTraComboBox.Items.Add(comboBoxModel);
            }
        }
    }
}
